#include "stdafx.h"
#include "ServiceImpCli.h"
#include "ServiceDataCli.h"
#include "Entity/EntityManagerCli.h"
#include "Scene/SceneManager.h"
#include "Message/MsgEntity.h"
#include "Message/MsgLogin.h"
#include "Message/MsgScene.h"
#include "Message/MsgLuaCommand.h"
#include "Message/MsgSession.h"
#include "Lua/LuaWrapper.h"

//extern __declspec(dllimport) Lua* gLua; 

ServiceImpCli::ServiceImpCli()
:m_wpSvcNW(NULL)
{
}

ServiceImpCli::~ServiceImpCli()
{

}

void
ServiceImpCli::init(ServiceData& data)
{
	ServiceDataCli* pSvcData = dynamic_cast<ServiceDataCli*>(&data);
	if (pSvcData)
	{
		m_wpSvcNW = pSvcData->m_wpSvcNW;
	}

	m_pSceneMgr = new SceneManager;

	m_pEntityMgr = new EntityManager;
}

void
ServiceImpCli::onStart()
{
	Lua::instance()->call("dofile('main.lua')");
	__super::onStart();
}

void
ServiceImpCli::onStop()
{
	__super::onStop();
}

void
ServiceImpCli::onTimer()
{
	//LOG_DEBUG(" Timeout, Client thread!!!");
	__super::onTimer();
}

void
ServiceImpCli::onDispatchMsg(IMsg* pMsg)
{
	//LOG_DEBUG("Dispatch Message, Client Thread!!!");
	
	int nMsgCate = pMsg->getMsgCate();
	int nMsgId = pMsg->getMsgID();

	//
	//	test vincent
	//
	if(Msg::MC_Login == pMsg->getMsgCate())
	{
		switch(nMsgId)
		{
		case MsgAuthResponse::Id:
			{
				MsgAuthResponse* msg = dynamic_cast<MsgAuthResponse*> (pMsg);
				if (msg)
				{
					//if (msg->m
					Lua::instance()->call("onAuthResponse()");
				}
			}
		case MsgSelectSvrResponse::Id:
			{
				MsgSelectSvrResponse* msg = dynamic_cast<MsgSelectSvrResponse*> (pMsg);
				if (msg)
				{
					//if (msg->m
					Lua::instance()->call("onSelectSvrResponse()");
				}
			}
		case MsgLoginResponse::Id:
			{
				MsgLoginResponse* msg = dynamic_cast<MsgLoginResponse*> (pMsg);
				if (msg)
				{
					Lua* lua = Lua::instance();
					lua->table("arg");
					int idx = 1;
					for(MsgLoginResponse::PlayerInfoList::iterator it=msg->playerlist.begin();
						it!=msg->playerlist.end();
						it++)
					{
						PlayerInfo & player = *it;
						
						lua->setfield(idx++, "{ pid = %d, name = \"%s\", hid= %d, sex = %d, headpic = %d }", player.pid, player.name.c_str(), (int)player.hid, (int)player.sex, (int)player.headpic);
					}

					lua->call("onLoginResponse", 1, 0);
				}
			}
			break;
		case MsgEnterGameResponse::Id:
			{
				Lua::instance()->call("onEnterGameResponse()");
			}
			break;
		case MsgCreatePlayerResponse::Id:
			{
				Lua::instance()->call("onCreatePlayerResponse()");
			}
			break;
		case MsgDeletePlayerResponse::Id:
			{
				Lua::instance()->call("onDeletePlayerResponse()");
			}
			break;
		}
	}
	else if (Msg::MC_Debug == pMsg->getMsgCate())
	{
		switch(nMsgId)
		{
		case MsgLuaCommand::Id:
			{
				MsgLuaCommand* msg = dynamic_cast<MsgLuaCommand*>(pMsg);
				if (msg && !msg->m_theCommandline.empty())
				{
					Lua::instance()->call(msg->m_theCommandline.c_str());
				}
				break;
			}
		}
	}
	else if(Msg::MC_Control == pMsg->getMsgCate())
	{
		switch(nMsgId)
		{
		case MsgSessionOpened::Id:
			{
				Lua::instance()->call("onSessionOpened()");
				break;
			}
		case MsgSessionClosed::Id:
			{
				Lua::instance()->call("onSessionClosed()");
				break;
			}
		}
	}
	else if (Msg::MC_Scene == pMsg->getMsgCate())
	{
		switch(nMsgId)
		{
		case MsgEnterDistrictResponse::Id:
			{
				MsgEnterDistrictResponse* msg = dynamic_cast<MsgEnterDistrictResponse*> (pMsg);
				if (msg)
				{
					Lua::instance()->call("onEnterDistrict()");
				}
				break;
			}
		case MsgEnterSceneResponse::Id:
			{
				MsgEnterSceneResponse* msg = dynamic_cast<MsgEnterSceneResponse*> (pMsg);
				if (msg)
				{
					Lua::instance()->setglobal("dsid", msg->dsid);
					Lua::instance()->call("onEnterScene()");
				}
				break;
			}
		}
	}
}
